/*
 * AddressOT.java
 *
 * $Id: AddressOT.java,v 1.7 2009-09-28 10:20:58 mario Exp $
 */
package org.ceteca.explica.core.partners;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.ceteca.explica.client.gui.ImageUtils;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.CountryOT;
import org.ceteca.explica.core.common.StateOT;
import org.ceteca.explica.core.util.GoogleMap;
import org.ceteca.explica.core.util.OperationResult;

/**
 * Store class to hold the data of an address (client address, supplier address,
 * warehouse address, etc.). <br/>
 * Responsibilities: <li>
 * <ul>
 * Store of address data for exchanging data among presentation, business and
 * data access layers.
 * </ul>
 * </li>. <br/>
 * 
 * @author Mario García García <mario@ceteca.es> Copyright (c) 2009 Fundación
 *         Centro Tecnolóxico da Carne This program is free software: you can
 *         redistribute it and/or modify it under the terms of the GNU General
 *         Public License as published by the Free Software Foundation, either
 *         version 3 of the License, or (at your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
public class AddressOT extends BaseVO implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Internal id of the address.
	 */
	private long idAddress;
	private String description;
	private String address;
	private String poBox;
	private String postalCode;
	private String city;
	private boolean billingAddress;
	private boolean shippingAddress;
	private CountryOT country;
	private StateOT state;

	
	/**
	 * Version of the address used by Hibernate for optimistic concurrency
	 * control.
	 */
	private Integer version = new Integer(0);

	public AddressOT() {
		super();


	}

	public long getIdAddress() {
		return idAddress;
	}

	public void setIdAddress(long idAddress) {
		this.idAddress = idAddress;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPoBox() {
		return poBox;
	}

	public void setPoBox(String poBox) {
		this.poBox = poBox;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public CountryOT getCountry() {
		return country;
	}

	public void setCountry(CountryOT country) {
		this.country = country;
	}

	/**
	 * Gets the version of the address used by Hibernate for optimistic
	 * concurrency control.
	 * 
	 * @return Integer, version of the group.
	 */
	public Integer getVersion() {
		return version;
	}

	/**
	 * Sets up the version of the address used by Hibernate for optimistic
	 * concurrency control.
	 * 
	 * @param version
	 *            Integer, version of the group.
	 */
	public void setVersion(Integer version) {
		this.version = version;
	}

	@Override
	public boolean equals(Long id) {
		return id != null && this.idAddress == id.longValue();
	}

	@Override
	public String getEntityId() {

		return InterfacePartners.EXPLICA_ENTITY_ADDRESS_ID;
	}

	@Override
	public long getInternalId() {

		return this.idAddress;
	}

	@Override
	public String getInternalValue() {

		return String.format("%s%s\n%s  %s (%s)\n%s", 
				this.getAddress(), 
				this.getPoBox()!=null?"\n"+this.getPoBox():"", 
				this.getPostalCode(), 
				this.getCity(), 
				this.getState().getInternalValue(),
				this.getCountry().getInternalValue());
//		return this.description;
	}

	public boolean isBillingAddress() {
		return billingAddress;
	}

	public void setBillingAddress(boolean billingAddress) {
		this.billingAddress = billingAddress;
	}

	public boolean isShippingAddress() {
		return shippingAddress;
	}

	public void setShippingAddress(boolean shippingAddress) {
		this.shippingAddress = shippingAddress;
	}

	
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();

		try {
			result.append("\n%%%%%%%%%%%% BEGIN ADDRESS %%%%%%%%%%%%%%");
			result.append("\nID: ");
			result.append(this.idAddress);
			result.append("\nDescription: ");
			result.append(this.description);

		} catch (Exception ex) {
			System.out.println("Error formatting Address!");
			ex.printStackTrace();
			return null;
		}

		return result.toString();
	}

	public StateOT getState() {
		return state;
	}

	public void setState(StateOT state) {
		this.state = state;
	}

	
	
	
	
	
	   @Override
	    public int hashCode() {
	        final HashCodeBuilder hcb = new HashCodeBuilder();
	        hcb.append(getDescription());
	        hcb.append(getAddress());
	        hcb.append(getCity());

	        return hcb.toHashCode();
	    }

	    
	    
	    @Override
	    public boolean equals(Object obj) {
	        boolean isEquals = false;
	        try {
	            final AddressOT other = (AddressOT)obj;
	            final EqualsBuilder eqb = new EqualsBuilder();
	            eqb.append(getDescription(), other.getDescription());
	            eqb.append(getAddress(), other.getAddress());
	            eqb.append(getCity(), other.getCity());
	            isEquals = eqb.isEquals();

	        } catch (Exception e) {
	            isEquals = false;
	        }

	        return isEquals;
	    }

	    
	    public java.awt.Image getMapImage(){
	    	GoogleMap googleMap=new GoogleMap(this, InterfaceExplica.GOOGLE_MAPS_DEFAULT_ZOOM, InterfaceExplica.GOOGLE_MAPS_DEFAULT_SIZE);
	    	OperationResult result=googleMap.build();
	    	if (result.getResult()==OperationResult.RESULT_OK)
	    		return ImageUtils.getInstance().convertImage(googleMap.getMapImage());
	    	else
	    		return null;
	    }
	
	

	
}
